//
//  PeoplePickerExampleViewController.m
//  PeoplePickerExample
//

#import "PeoplePickerExampleViewController.h"

@implementation PeoplePickerExampleViewController

- (void)logPerson:(ABRecordRef)person {
	CFStringRef name = ABRecordCopyCompositeName(person);
	ABRecordID recId = ABRecordGetRecordID(person);
	NSLog(@"Person Name: %@ RecordID: %d", name, recId);
}

- (void)showPeoplePicker:(id)sender {
	[self presentModalViewController:picker animated:YES];
}

- (void)createUI {
	showPicker = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[showPicker addTarget:self action:@selector(showPeoplePicker:) forControlEvents:UIControlEventTouchUpInside];
	[showPicker setFrame:CGRectMake(190, 10, 120, 38)];
	[showPicker setTitle:@"People Picker" forState:UIControlStateNormal];
	[self.view addSubview:showPicker];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	[self createUI];
	
	picker = [[ABPeoplePickerNavigationController alloc] init];
	picker.peoplePickerDelegate = self;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
	  shouldContinueAfterSelectingPerson:(ABRecordRef)person {
	[peoplePicker dismissModalViewControllerAnimated:YES];
	[self logPerson:person];
	return NO;
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
	[peoplePicker dismissModalViewControllerAnimated:YES];
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
	  shouldContinueAfterSelectingPerson:(ABRecordRef)person
								property:(ABPropertyID)property
							  identifier:(ABMultiValueIdentifier)identifier {
	[peoplePicker dismissModalViewControllerAnimated:YES];
	[self logPerson:person];
	return NO;
}

- (void)dealloc {
	[picker release];
    [super dealloc];
}

@end
